package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.clues.service.ISysMenuService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;

/**
 * 登录验证
 * 
 * 
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    //@ApiOperation("获取路由信息")
   /* @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }*/
    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    //@ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public String getRouters()
    {
        return "{\"msg\":\"操作成功\",\"code\":200,\"data\":[{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"首页\",\"icon\":\"icon_menu_home\",\"noCache\":false},\"children\":[{\"name\":\"Index\",\"path\":\"index\",\"hidden\":false,\"component\":\"indexHome\",\"meta\":{\"title\":\"首页\",\"icon\":\"icon_menu_home\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"线索管理\",\"icon\":\"icon_menu_xsgl\",\"noCache\":false},\"children\":[{\"name\":\"Clue\",\"path\":\"clue\",\"hidden\":false,\"component\":\"clues/clue/index\",\"meta\":{\"title\":\"线索管理\",\"icon\":\"icon_menu_xsgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"商机管理\",\"icon\":\"icon_menu_sjgl\",\"noCache\":false},\"children\":[{\"name\":\"Business\",\"path\":\"business\",\"hidden\":false,\"component\":\"clues/business/index\",\"meta\":{\"title\":\"商机管理\",\"icon\":\"icon_menu_sjgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"合同管理\",\"icon\":\"icon_menu_htgl\",\"noCache\":false},\"children\":[{\"name\":\"Contract\",\"path\":\"contract\",\"hidden\":false,\"component\":\"clues/contract/index\",\"meta\":{\"title\":\"合同管理\",\"icon\":\"icon_menu_htgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"活动管理\",\"icon\":\"icon_menu_hdgl\",\"noCache\":false},\"children\":[{\"name\":\"Activity\",\"path\":\"activity\",\"hidden\":false,\"component\":\"clues/activity/index\",\"meta\":{\"title\":\"活动管理\",\"icon\":\"icon_menu_hdgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"课程管理\",\"icon\":\"icon_menu_kcgl\",\"noCache\":false},\"children\":[{\"name\":\"Course\",\"path\":\"course\",\"hidden\":false,\"component\":\"clues/course/index\",\"meta\":{\"title\":\"课程管理\",\"icon\":\"icon_menu_kcgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"统计分析\",\"icon\":\"icon_menu_tjfx\",\"noCache\":false},\"children\":[{\"name\":\"CountAnalysis\",\"path\":\"countAnalysis\",\"hidden\":false,\"component\":\"clues/countAnalysis/index\",\"meta\":{\"title\":\"统计分析\",\"icon\":\"icon_menu_tjfx\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"转派管理\",\"icon\":\"example\",\"noCache\":false},\"children\":[{\"name\":\"TransferManage\",\"path\":\"transferManage\",\"hidden\":false,\"component\":\"clues/transferManage/index\",\"meta\":{\"title\":\"转派管理\",\"icon\":\"example\",\"noCache\":false}}]},{\"name\":\"System\",\"path\":\"/system\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"系统管理\",\"icon\":\"icon_menu_xtgl\",\"noCache\":false},\"children\":[{\"name\":\"Permission\",\"path\":\"permission\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"权限管理\",\"icon\":\"icon_menu_qxgl\",\"noCache\":false},\"children\":[{\"name\":\"User\",\"path\":\"user\",\"hidden\":false,\"component\":\"system/user/index\",\"meta\":{\"title\":\"用户管理\",\"icon\":\"user\",\"noCache\":false}},{\"name\":\"Role\",\"path\":\"role\",\"hidden\":false,\"component\":\"system/role/index\",\"meta\":{\"title\":\"角色管理\",\"icon\":\"peoples\",\"noCache\":false}},{\"name\":\"Menu\",\"path\":\"menu\",\"hidden\":false,\"component\":\"system/menu/index\",\"meta\":{\"title\":\"菜单管理\",\"icon\":\"tree-table\",\"noCache\":false}},{\"name\":\"Dept\",\"path\":\"dept\",\"hidden\":false,\"component\":\"system/dept/index\",\"meta\":{\"title\":\"部门管理\",\"icon\":\"tree\",\"noCache\":false}},{\"name\":\"Post\",\"path\":\"post\",\"hidden\":false,\"component\":\"system/post/index\",\"meta\":{\"title\":\"岗位管理\",\"icon\":\"post\",\"noCache\":false}}]},{\"name\":\"Dict\",\"path\":\"dict\",\"hidden\":false,\"component\":\"system/dict/index\",\"meta\":{\"title\":\"字典管理\",\"icon\":\"icon_menu_zdgl\",\"noCache\":false}},{\"name\":\"Clew\",\"path\":\"clew\",\"hidden\":false,\"component\":\"system/clew/index\",\"meta\":{\"title\":\"线索配置\",\"icon\":\"icon_menu_xspz\",\"noCache\":false}},{\"name\":\"Opportunity\",\"path\":\"opportunity\",\"hidden\":false,\"component\":\"system/opportunity/index\",\"meta\":{\"title\":\"商机配置\",\"icon\":\"icon_menu_sjpz\",\"noCache\":false}},{\"name\":\"NoticeCenter\",\"path\":\"noticeCenter\",\"hidden\":false,\"component\":\"system/noticeCenter/index\",\"meta\":{\"title\":\"通知中心\",\"icon\":\"icon_menu_tzzx\",\"noCache\":false}},{\"name\":\"Log\",\"path\":\"log\",\"hidden\":true,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"日志管理\",\"icon\":\"log\",\"noCache\":false},\"children\":[{\"name\":\"Operlog\",\"path\":\"operlog\",\"hidden\":false,\"component\":\"monitor/operlog/index\",\"meta\":{\"title\":\"操作日志\",\"icon\":\"form\",\"noCache\":false}},{\"name\":\"Logininfor\",\"path\":\"logininfor\",\"hidden\":false,\"component\":\"monitor/logininfor/index\",\"meta\":{\"title\":\"登录日志\",\"icon\":\"logininfor\",\"noCache\":false}}]},{\"name\":\"SystemLog\",\"path\":\"systemLog\",\"hidden\":false,\"component\":\"system/systemLog/index\",\"meta\":{\"title\":\"系统日志\",\"icon\":\"icon_menu_xtrz\",\"noCache\":false}}]}]}";
    }
}
